package com.hs.egova.controller;

import com.hs.egova.base.ResultInfo;
import com.hs.egova.exception.EgovaCommonException;
import com.hs.egova.mapping.PartyOrg;
import com.hs.egova.mapping.PartyOrgVo;
import com.hs.egova.service.UserManager;
import com.hs.egova.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;

/**
 * @author husheng
 * @version v1.0
 * @date 2021/7/27 16:27
 * @email husheng@egova.com.cn
 */
@RestController
@RequestMapping("/v3")
@Api(tags = "新版用户操作")
@RequiredArgsConstructor
@Slf4j
@Validated
public class UserNewController {

    private final UserManager userManager;

    @ApiOperation("根据用户姓名查找用户")
    @PostMapping("/test1")
    public ResultInfo listUser(@Valid @RequestBody UserVo userVo) {
        log.info("name: {}", userVo.getUserName());
        return   userManager.getUserByName(userVo.getUserName());

    }

    @ApiOperation("党建")
    @GetMapping("/party")
    public ResultInfo getPartyOrg(@Valid PartyOrgVo partyOrgVo){
        ResultInfo resultInfo = new ResultInfo(true);
        PartyOrg partyOrg = new PartyOrg();
        resultInfo.setData("data", partyOrgVo);
        return resultInfo;
    }

    @ApiOperation("异常抛出")
    @GetMapping("exception")
    public ResultInfo testException(String name) {
        if ("husheng".equals(name)) {
            throw new EgovaCommonException("姓名不能为husheng");
        }
        return new ResultInfo(true);
    }

    @GetMapping("/test2")
    @ApiOperation("根据用户姓名查找")
    public ResultInfo listUser1(@Valid UserVo userVo) {
        log.info("name: {}", userVo.getUserName());
        return userManager.getUserByUserName(userVo.getUserName());
    }


    @GetMapping("/test3")
    @ApiOperation("测试校验")
    public ResultInfo listUser2(@RequestParam @NotNull( message = "姓名不能为空")  String name,@RequestParam @AssertFalse(message = "初始化必须为false") boolean flag) {
        log.info("name: {}, flag: {}", name, flag);
        return new ResultInfo(true);
    }

    @ApiOperation("根据Iids查找人")
    @GetMapping("/test4")
    public ResultInfo listUser3(@RequestParam("ids") Integer[] ids) {
        log.info("ids: {} length: {}", ids.toString(), ids.length);
        return userManager.getUserByIds(ids);
    }
}
